package gold.digger;

import gold.utils.InputUtil;

/**
 * Created by fanzhenyu02 on 2021/12/10.
 * common problem solver template.
 */
public class LC1208 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public int equalSubstring(String s, String t, int maxCost) {
            int left = 0, right = 0, ans = 0, curCost = 0;
            while (right < s.length()) {
                curCost += Math.abs(s.charAt(right) - t.charAt(right));
                ++right;
                if (curCost <= maxCost) {
                    ans = Math.max(ans, right - left);
                    continue;
                }

                // 当前已经不满足，需要缩小窗口
                while (curCost > maxCost && left <= right) {
                    curCost -= Math.abs(s.charAt(left) - t.charAt(left));
                    left++;
                }
            }

            return Math.max(ans, right - left);
        }
    }

    public void run() {
        Solution solution = new Solution();
//        System.out.println(solution.equalSubstring("abcd", "bcdf", 3));
//        System.out.println(solution.equalSubstring("abcd", "cdef", 3));
        System.out.println(solution.equalSubstring("abcd", "acde", 0));
    }

    public static void main(String[] args) throws Exception {
        LC1208 an = new LC1208();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
